<template>
  <div ref="app_sec" class="app-sec" style="width: 100%;
  height: 50%;"></div>
</template>

<script>
import * as echarts from "echarts";
import { mapState } from 'vuex';
export default {
  data() {
    return {
      chartInstance: null,
    };
  },
  computed: {
 ...mapState(['theme'])
  },
  methods: {
    initChart() {
      // 初始化对象
     this.chartInstance = echarts.init(this.$refs.app_sec,this.theme);
      let option = {
        color: ["#21ffeb", "#36adff"],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            label: {
              backgroundColor: "#6a7985",
            },
          },
        },
        legend: {
          bottom: "3%",
         textStyle: {
            color: "rgba(255,255,255,0.8)",
          },
        },

        grid: {
          left: "3%",
          right: "0%",
          bottom:'15%',
          top:'8%',
          containLabel: true,
        },

        xAxis: [
          {
            type: "category",
            boundaryGap: false,
            data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
            axisTick: {
              show: false,
            },
            axisLine: {
              show: false,
                },
            boundaryGap: true,
                axisLabel: {
              show: true, color: "rgba(255,255,255,0.5)",
  
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            splitLine: {
              lineStyle: {
                color: "#1a1f6d",
                width: 1,
              },
            },
               axisLabel: {
              show: true, color: "rgba(255,255,255,0.5)",
  
            },
            scale: true,
          },
        ],
        calculable: true,
        series: [
          {
            name: "Rainfall",
            barWidth: 10,
            type: "bar",
            data: [
         200,203,209,209,240,260,300
            ],
            symbol: "circle",
          },
          {
            name: "Evaporation",
            barWidth: 10,
            type: "bar",
            data: [
              210,213,219,218,200,214,320
            ],
            symbol: "circle",
          },
        ],
      };
      this.chartInstance.setOption(option);
    },
  },
  mounted() {
    this.initChart();
        // 添加随页面变化而变化
    window.addEventListener('resize', () => {
       this.chartInstance.resize()
    })
  },
      destroyed () {
    window.removeEventListener("resize", () => {
      this.chartInstance.resize();
    });
  },
      destroyed () {
    window.removeEventListener("resize", () => {
      this.chartInstance.resize();
    });
  },
      watch: {
    theme(newValue, oldValue) {
      this.chartInstance.dispose()
      this.initChart()

    }
  },
};
</script>

<style lang="scss" scoped>
.app-sec {
  width: 100%;
  height: 50%;
}
</style>